package hasthi.tools;

import hasthi.common.BaseContext;
import hasthi.common.DistributedLogger;
import hasthi.common.HasthiException;
import hasthi.common.HasthiException.FaultCode;
import hasthi.container.ManagmentNode;
import hasthi.container.p2p.BootstrapNode;
import hasthi.manager.gui.ManagementGUI;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class SingleJvmManagmentNodeSet {
    private Map<String, String> options = new HashMap<String, String>();
    
    private boolean needgui = false;
    private boolean needbootstrap = false;
    private int managerCount = 10;
    private int serviceCount = 20;
    private boolean onlyServices = false; 
    private String configFile;
    
    
    public void startNodeSet() throws HasthiException{
        try {
            
            BaseContext baseContext;
            if(configFile != null){
                Properties properties = new Properties();
                FileInputStream in = new FileInputStream(configFile);
                properties.load(in);
                in.close();
                baseContext = new BaseContext(properties);
            }else{
                baseContext = new BaseContext();
            }
            
            if(needgui){
                ManagementGUI gui = new ManagementGUI();
                Thread.sleep(2000);
                gui.initGUI(null);
            }
            if(needbootstrap){
                BootstrapNode bootstrapNode = new BootstrapNode(baseContext);
                bootstrapNode.startNode();
            }    
            Thread.sleep(1000);
            
            ManagmentNode node1 = new ManagmentNode(baseContext);
            node1.startManagmentNode();

            
            for(int i = 0;i<managerCount-1;i++){
                ManagmentNode node = new ManagmentNode(baseContext);
                node.startManagmentNode();
            }
            // Create three managers

            // Create 5 Services
            //        
//            for(int i = 0;i<serviceCount;i++){
//                DummyService dummyService = new DummyService();
//                dummyService.start();
//            }
            
            Thread.sleep(10000);
            DistributedLogger dLogger = new DistributedLogger(baseContext);
            dLogger.info("Node Set Started");
        } catch (InterruptedException e) {
            throw new HasthiException(e,FaultCode.InitalizationError);
        } catch (FileNotFoundException e) {
            throw new HasthiException(e,FaultCode.InitalizationError);
        } catch (IOException e) {
            throw new HasthiException(e,FaultCode.InitalizationError);
        }
    }


    public void setOptions(Map<String, String> options) {
        this.options = options;
    }


    public void setNeedgui(boolean needgui) {
        this.needgui = needgui;
    }


    public void setNeedbootstrap(boolean needbootstrap) {
        this.needbootstrap = needbootstrap;
    }


    public void setManagerCount(int managerCount) {
        this.managerCount = managerCount;
    }


    public void setServiceCount(int serviceCount) {
        this.serviceCount = serviceCount;
    }
    
    public void loadFromArgs(String[] args){
        List<String> params = new ArrayList<String>(2);
        
        for (int i = 0; i < args.length; i++) {
            if(args[i].equals("-X")){
                needgui = true;
            }else if(args[i].equals("-B")){
                needbootstrap = true;
            }else if(args[i].equals("-S")){
                onlyServices = true;
            }else if(args[i].startsWith("-config=")){
                configFile = args[i].substring(8,args[i].length());               
            }else {
                params.add(args[i]);
            }
        }
        if(!onlyServices){
            if(params.size() >= 1){
                managerCount = Integer.parseInt(params.get(0));
            }
            if(params.size() == 2){
                serviceCount = Integer.parseInt(params.get(1));
            }
        }else{
            if(params.size() == 1){
                serviceCount = Integer.parseInt(params.get(0));
            }
        }
    }
    
    
    public static void main(String[] args) throws Exception {
        SingleJvmManagmentNodeSet nodeSet = new SingleJvmManagmentNodeSet();
        nodeSet.loadFromArgs(args);
        nodeSet.startNodeSet();
        //Thread.sleep(15*60*1000);
        //System.exit(0);
        System.in.read();
        System.out.println("Enter y to kill all managers");
        char c = (char)System.in.read();
        while(c != 'y'){
            System.out.println("Enter y to kill all managers");
            c = (char)System.in.read();
        }
        System.exit(0);
    }
}
